<template>
  <div>
    <!--    添加角色-->
    <div id="form">
      <el-form label-width="280px">
        <el-form-item label="角色名称">
          <el-input v-model="tableData.name" placeholder="请输入权限标识字符串" style="width:100% "></el-input>
        </el-form-item>
        <el-form-item label="角色描述" style="margin-top: 50px">
          <el-input type="textarea" v-model="tableData.descp" placeholder="请输入权限描述"></el-input>
        </el-form-item>
        <el-form-item label="角色权限">
          <el-tree ref="tree" style="border: 1px solid #DCDFE6;border-radius: 4px"
                   :props="props" :data="permissionList"
                   node-key="id" :check-strictly="true"
                   show-checkbox @check-change="handleCheckChange">
          </el-tree>
        </el-form-item>
        <el-form-item style="margin-top: 50px;">
          <el-button style="margin-right: 30px" type="primary" @click="onSubmit">立即添加</el-button>
          <el-button type="info" @click="onClean">重新输入</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import permissionApi from '@/api/permission'
import roleApi from '@/api/role'
import qs from "qs";

export default {
  name: "add",
  data() {
    return {
      props: {
        label: 'tag',
      },
      tableData: {
        name: "",
        descp: "",
        permissionIdList: [],
      },
      permissionList: []

    }
  },
  methods: {
    //树形改变之后
    handleCheckChange(data, checked, indeterminate) {
      this.tableData.permissionIdList = this.$refs.tree.getCheckedKeys();
    },
    //查询树形的权限信息
    selectTreePermissionList() {
      permissionApi.selectTreeList().then(resp => {
        this.permissionList = resp.data;
      }).catch(() => {
        this.$message.error('查询树形权限出错')
      })
    },
    //点击添加
    onSubmit() {
      roleApi.addRole(qs.stringify(this.tableData, {indices: false})).then(resp => {
        if (resp.code === 6666) {
          this.$message.success("添加成功！");
          this.onClean();
          this.$router.push('/rbac/role/list')
        } else {
          this.$message.info(resp.msg)
        }
      }).catch(() => {
        this.$message.error("添加出错！")
      })
    },
    //清楚已输入的数据
    onClean() {
      this.tableData = {}
    }
  },
  mounted() {
    this.selectTreePermissionList();
  }
}
</script>

<style scoped>
#form {
  margin-top: 120px;
  width: 680px;
}
</style>
